using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace QUAN_LY_NGAN_HANG_CAU_HOI_TRAC_NGHIEM
{
    class ProviderDao
    {        
        private SqlConnection m_Connection;
        private SqlDataAdapter  m_DataAdapter;
        private SqlCommand m_Command;
        public DataTable ExecuteQuery(String Sql)
        {
            String ConnectionStringSql;
            ConnectionStringSql = "Data Source=.\\USER-PC\\SQLEXPRESS;AttachDbFilename=C:\\QUAN_LY_CAU_HOI_TRAC_NGHIEM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            m_Connection = new SqlConnection(ConnectionStringSql);
            DataTable dt = new DataTable();             
            try
            {
                m_Connection.Open();//thiet lap ket noi
                m_DataAdapter = new SqlDataAdapter(Sql, m_Connection);                
                m_DataAdapter.Fill(dt);
                m_Connection.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show("Error :" + e.ToString());
                return null;
            }                        
            return dt;

        }
        public void ExecuteDML(String Sql)
        {
            String ConnectionStringSql;
            ConnectionStringSql = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\QLMA_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            m_Connection = new SqlConnection(ConnectionStringSql);
            try
            {
                m_Connection.Open();//thiet lap ket noi
                m_Command = new SqlCommand(Sql, m_Connection);
                m_Command.CommandType = CommandType.Text;
                m_Command.ExecuteNonQuery();
                m_Connection.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show("Error :" + e.ToString());
            }
        }
        /*
        private OleDbConnection m_Connection;
        private OleDbCommand m_Command;
        private OleDbDataAdapter m_DataAdapter;
        private String apppath;
        public DataTable ExecuteQuery(String Sql)
        {
            String ConnectionString,ConnectionStringSql;
            apppath = Application.StartupPath;
            apppath = apppath.Remove(apppath.Length - 10, 10);
            ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + apppath + "/dulieu.mdb";
            m_Connection = new OleDbConnection(ConnectionStringSql);
            m_Connection.Open();//thiet lap ket noi

            m_DataAdapter = new OleDbDataAdapter(Sql, m_Connection);
            DataTable dt = new DataTable();

            m_DataAdapter.Fill(dt);
            m_Connection.Close();
            return dt;

        }
        public void ExecuteDML(String Sql)
        {
            String ConnectionString;
            apppath = Application.StartupPath;
            apppath = apppath.Remove(apppath.Length - 10, 10);
            ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + apppath + "/dulieu.mdb";

            m_Connection = new OleDbConnection(ConnectionString);
            m_Connection.Open();

            m_Command = new OleDbCommand(Sql, m_Connection);
            m_Command.CommandType = CommandType.Text;
            m_Command.ExecuteNonQuery();
            m_Connection.Close();
        }
         */
    }
}
